package com.yootk;

import java.util.concurrent.atomic.AtomicStampedReference;

class Book {
    private String title;
    private double price;

    public Book(String title, double price) {
        this.title = title;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "【图书】名称 = " + title + "，价格 = " + price;
    }
}

public class YootkDemo3 {
    public static void main(String[] args) throws Exception {
        Book book = new Book("Java就业编程实战", 69.8);    // 实例化Book类对象
        // 由于AtomicStampedReference需要提供有版本号，所以在初始化时定义版本编号为1
        AtomicStampedReference<Book> ref = new AtomicStampedReference<Book>(book, 1);
        // 在进行CAS操作时除了要设置替换内容之外，也需要设置正确的版本号，否则无法替换
        ref.compareAndSet(book, new Book("Spring就业编程实战", 67.8), 1, 2); // CAS操作
        System.out.println(ref.getReference()); // 获取引用数据
        System.out.println("当前版本号：" + ref.getStamp());
    }
}